package com.lc.hongtai.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lc.hongtai.MyApplication;
import com.lc.hongtai.R;
import com.lc.hongtai.activity.InvestmentActivity;
import com.lc.hongtai.conn.JsonInvest;
import com.lc.hongtai.view.ProgressBar;
import com.zcx.helper.bound.BoundViewHelper;

import java.util.List;

/**
 * Created by huangjinlong on 2016/3/4.
 */
public class RecyclerAdapter extends RecyclerView.Adapter<ViewHolder> {
    public final static int TYPE_ONE = 0;
    public final static int TYPE_TWO = 1;
    private Context mContext;
    private List<JsonInvest.Info.ListContent> list;
    private OnItemClickListener onItemClickListener;
    private boolean loadMore = false;

    public RecyclerAdapter(Context context, List<JsonInvest.Info.ListContent> list) {
        this.mContext = context;
        this.list = list;
    }


    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }


    public interface OnItemClickListener {
        void OnItemClick(View view, int position);

        void OnItemLongClick(View view, int position);
    }


    @Override
    public int getItemViewType(int position) {


        if (position == 0) {
            return TYPE_ONE;
        } else {
            return TYPE_TWO;
        }


    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        ViewHolder holder = null;
        switch (viewType) {
            case TYPE_ONE:
                //  view = LayoutInflater.from(mContext).inflate(R.layout.item_invest, parent, false);
                view = BoundViewHelper.boundView(this, MyApplication.scaleScreenHelper.loadView((ViewGroup) LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_invest, null)));
                holder = new ViewHolderOne(view);
                break;
            case TYPE_TWO:
                // view = LayoutInflater.from(mContext).inflate(android.R.layout.simple_list_item_1, parent, false);
                view = BoundViewHelper.boundView(this, MyApplication.scaleScreenHelper.loadView((ViewGroup) LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_investlist, null)));
                holder = new ViewHolderTwo(view);
                break;
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {


        switch (getItemViewType(position)) {
            case TYPE_ONE:
                final ViewHolderOne holderOne = (ViewHolderOne) holder;
                //  onItemEventClick(holderOne);
                holderOne.tv_name.setText(list.get(position).borrow_name);
                holderOne.tv_rate.setText(list.get(position).borrow_interest_rate);
                //  holderOne.text_progress.setText(list.get(position).progress);
                //    holderOne.text_during.setText(list.get(position).borrow_duration + list.get(position).borrow_duration_cn);
                //      holderOne.text_shengyu.setText(list.get(position).borrow_duration_cn);
                holderOne.tv_time.setText(list.get(position).need);
                holderOne.tv_rate2.setText(list.get(position).borrow_duration + list.get(position).borrow_duration_cn);
                holderOne.tv_sumMoney.setText(list.get(position).progress);
                break;
            case TYPE_TWO:
                ViewHolderTwo holderTwo = (ViewHolderTwo) holder;
                holderTwo.tv_name.setText(list.get(position).borrow_name);
                holderTwo.tv_time.setText(list.get(position).need);
                holderTwo.tv_rate.setText(list.get(position).borrow_duration + list.get(position).borrow_duration_cn);
                holderTwo.tv_sumMoney.setText(list.get(position).progress);
                holderTwo.progress_horizontal.setProgress(Double.valueOf(list.get(position).progress));
                holderTwo.tv_progress.setText(list.get(position).progress+"%");
                break;
        }


        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (list.isEmpty() | list.size() == 0) {

                } else {
                    Intent intent = new Intent();
                    intent.putExtra("id", list.get(position).id);
                    intent.setClass(mContext, InvestmentActivity.class);
                    mContext.startActivity(intent);
                }


            }
        });
    }

    @Override
    public int getItemCount() {
   /*     if (beans.size() > 3) {
            if (!loadMore) {
                return 4;
            }
        }*/
        return list.size();
    }


    class ViewHolderOne extends RecyclerView.ViewHolder {
        TextView tv_name;
        TextView tv_rate;

        TextView text_progress;


        //剩余时间
        TextView text_shengyu;

        //起投金额
        TextView tv_time;
        //期限
        TextView tv_rate2;
        //进度
        TextView tv_sumMoney;

        public ViewHolderOne(View itemView) {
            super(itemView);
            tv_name = (TextView) itemView.findViewById(R.id.tv_name);
            tv_rate = (TextView) itemView.findViewById(R.id.tv_rate);
            text_progress = (TextView) itemView.findViewById(R.id.text_progress);
            tv_rate2 = (TextView) itemView.findViewById(R.id.tv_rate2);
            text_shengyu = (TextView) itemView.findViewById(R.id.text_shengyu);

            tv_time = (TextView) itemView.findViewById(R.id.tv_time);
            tv_sumMoney = (TextView) itemView.findViewById(R.id.tv_sumMoney);
        }
    }

    class ViewHolderTwo extends RecyclerView.ViewHolder {
        TextView tv_name;
        //剩余时间
        TextView tv_time;
        TextView tv_rate;
        //进度
        TextView tv_sumMoney;

        ProgressBar progress_horizontal;

        TextView tv_progress;

        public ViewHolderTwo(View itemView) {
            super(itemView);
            tv_name = (TextView) itemView.findViewById(R.id.tv_name);
            tv_time = (TextView) itemView.findViewById(R.id.tv_time);
            tv_rate = (TextView) itemView.findViewById(R.id.tv_rate);
            tv_sumMoney = (TextView) itemView.findViewById(R.id.tv_sumMoney);
            progress_horizontal = (ProgressBar) itemView.findViewById(R.id.progress_horizontal);
            tv_progress = (TextView) itemView.findViewById(R.id.tv_progress);
        }
    }

    protected void onItemEventClick(ViewHolder holder) {
        final int position = holder.getLayoutPosition();
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.OnItemClick(v, position);
            }
        });
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                onItemClickListener.OnItemLongClick(v, position);
                return true;
            }
        });
    }

}
